package com.wing.product.controller.admin;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.annotation.RequestObjectFieldIgnore;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.device.model.response.DeviceWorkshopVO;
import com.wing.product.model.entity.Product;
import com.wing.product.model.request.ProductDispatchWorkInfoForm;
import com.wing.product.model.response.ProductDispatchWorkUserVO;
import com.wing.product.model.response.ProductDispatchWorkVO;
import com.wing.product.service.ProductDispatchWorkService;
import com.wing.system.model.response.DingTalkAttendanceGroupUseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cwm 2025-02-20
 */
@Validated
@Api(value = "Admin-派工管理", tags = "#/product/productDispatchWork/productDispatchWorkList")
@RestController("ProductDispatchWorkController")
@RequestMapping("product/admin/productDispatchWork")
@RequiredArgsConstructor
public class ProductDispatchWorkController {

    @Autowired
    ProductDispatchWorkService productDispatchWorkService;

    @ApiOperation(value = "查询所有车间信息", notes = "查询")
    @GetMapping("/getDeviceWorkshopList")
    public JsonResult<List<DeviceWorkshopVO>> getDeviceWorkshopList() {
        List<DeviceWorkshopVO> list = productDispatchWorkService.getDeviceWorkshopList();
        return JsonResult.success(list);
    }



    @ApiOperation(value = "查询所有钉钉同步过来的考勤组", notes = "查询")
    @GetMapping("/selAllGroupList")
    public JsonResult<List<DingTalkAttendanceGroupUseVo>> selAllGroupList() {
        List<DingTalkAttendanceGroupUseVo> list = productDispatchWorkService.selAllGroupList();
        return JsonResult.success(list);
    }


    @ApiOperation(value = "获取工位和考勤相关信息", notes = "搜索-任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/getCheckDeviceWorkToAttendanceTime")
    public PageResult<ProductDispatchWorkVO> checkDeviceWorkToAttendanceTime(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,@RequestParam("workshopId") String workshopId
            ,@RequestParam("groupId") String groupId
    ) {
        Page<ProductDispatchWorkVO> list = productDispatchWorkService.checkDeviceWorkToAttendanceTime(workshopId,groupId);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }




    @ApiOperation(value = "获取岗位和考勤交集的人员信息", notes = "查询")
    @GetMapping("/getDispatchWorkUserInfo")
    public PageResult<List<ProductDispatchWorkUserVO>> getDispatchWorkUserInfo(@RequestParam(name = "postId") String postId,
                                                                   @RequestParam(name = "groupId") String groupId,
                                                                   @RequestParam(name = "dispatchWorkDate") String dispatchWorkDate) {
        List<ProductDispatchWorkUserVO> dispatchWorkUserInfo = productDispatchWorkService.getDispatchWorkUserInfo(postId, groupId, dispatchWorkDate);
        Page page=new Page();
        page.setRecords(dispatchWorkUserInfo);
        return new PageResult<>(page.getRecords(), page.getTotal());
    }



    @ApiOperation(value = "获取派工列表相关信息", notes = "搜索-任务")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<ProductDispatchWorkVO> selDispatchListInfo(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
            ,String startTime
            ,String endTime
            ,String dispatchWorkNumber
            ,String creatorName
            ,String dispatchWorkStatus
    ) {
        Page<Product> pg = new Page<>(page, limit);
        Map map = new HashMap();
        map.put("startTime",startTime);
        map.put("endTime",endTime);
        map.put("dispatchWorkNumber",dispatchWorkNumber);
        map.put("creatorName",creatorName);
        map.put("dispatchWorkStatus",dispatchWorkStatus);
        Page<ProductDispatchWorkVO> list = productDispatchWorkService.selDispatchListInfo(pg,map);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    @ApiOperation(value = "添加派工数据", notes = "查询")
    @PostMapping("/saveDispatchWorkInfo")
    public JsonResult saveDispatchWorkInfo(@RequestBody ProductDispatchWorkInfoForm productDispatchWorkInfoForm) {
        try {
            return productDispatchWorkService.saveDispatchWorkInfo(productDispatchWorkInfoForm);
        } catch (Exception e) {
            return JsonResult.fail(e.getMessage());
        }
    }



    @ApiOperation(value = "获取顶部岗位数和工位数和班次数", notes = "查询")
    @GetMapping("/getTopDispatchCount")
    public JsonResult getTopDispatchCount(@RequestParam("workshopId") String workshopId,@RequestParam("groupId") String groupId) {
        try {
            return JsonResult.success(productDispatchWorkService.getTopDispatchCount(workshopId,groupId));
        } catch (Exception e) {
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "编辑时候获取详情", notes = "查询")
    @GetMapping("/getDispatchWorkInfoEditList")
    public JsonResult getDispatchWorkInfoEditList(@RequestParam("dispatchWorkNumber") String dispatchWorkNumber,@RequestParam("id") Long id) {
        try {
            return JsonResult.success(productDispatchWorkService.getDispatchWorkInfoEditList(dispatchWorkNumber,id));
        } catch (Exception e) {
            return JsonResult.fail(e.getMessage());
        }
    }

}
